
import matplotlib.pyplot as plt
import numpy as np

# 将图表绘制成 1x2 的图片区域
def fun1():
    # plot 1
    xpoints = np.array([0, 6])
    ypoints = np.array([0, 100])

    plt.subplot(1, 2, 1)
    plt.plot(xpoints, ypoints)
    plt.title("plot 1")

    # plot 2
    x = np.array([1, 2, 3, 4])
    y = np.array([1, 4, 9, 16])

    plt.subplot(1, 2, 2)
    plt.plot(x, y)
    plt.title("plot 2")

    plt.suptitle("Python subplot Test")
    plt.show()

# 将图表绘制成 2x2 的图片区域
def fun2():
    # plot 1
    x = np.array([0, 6])
    y = np.array([0, 100])

    plt.subplot(2, 2, 1)
    plt.plot(x, y)
    plt.title("plot 1")

    # plot 2
    x = np.array([1, 2, 3, 4])
    y = np.array([1, 4, 9, 16])

    plt.subplot(2, 2, 2)
    plt.plot(x, y)
    plt.title("plot 2")

    # plot 3
    x = np.array([1, 2, 3, 4])
    y = np.array([3, 5, 7, 9])

    plt.subplot(2, 2, 3)
    plt.plot(x, y)
    plt.title("plot 3")

    # plot 4
    x = np.array([1, 2, 3, 4])
    y = np.array([4, 5, 6, 7])

    plt.subplot(2, 2, 4)
    plt.plot(x, y)
    plt.title("plot 4")

    plt.suptitle("Python subplot Test")
    plt.show()

# subplots
def fun3():
    # 创建一些测试数据 -- 图1
    x = np.linspace(0, 2 * np.pi, 400)
    y = np.sin(x ** 2)

    # 创建一个画像和子图 -- 图2
    fig, ax = plt.subplots()
    ax.plot(x, y)
    ax.set_title('Simple plot')

    # 创建两个子图 -- 图3
    f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
    ax1.plot(x, y)
    ax1.set_title('Sharing Y axis')
    ax2.scatter(x, y)

    # 创建四个子图 -- 图4
    fig, axs = plt.subplots(2, 2, subplot_kw=dict(projection="polar"))
    axs[0, 0].plot(x, y)
    axs[1, 1].scatter(x, y)

    plt.show()

if __name__ == '__main__':
    fun3()